package cn.ingenic.glasssync.control;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.util.Log;
import cn.ingenic.glasssync.services.SyncModule;
import cn.ingenic.glasssync.services.SyncData;

public class ControlModule extends SyncModule {

        private static final String TAG = "ControlModule";
        private static final String MODULE_NAME = "ctrl_module";

        private final String KEY_TYPE = "type";

        private final int T_REST_APACHE = 0; // restart apache

        private ControlModule(String name, Context context) {
                super(name, context);
        }

        public static ControlModule getInstance (Context context) {
                return new ControlModule(MODULE_NAME, context);
        }

        @Override
        protected void onRetrive(SyncData data) {
                super.onRetrive(data);
                int type = data.getInt(KEY_TYPE);
                Log.d(TAG, "onRetrive() in, type = " + type);
                switch (type) {
                case T_REST_APACHE: {
                        File file = new File("/data/local/tmp/rstapache.fifo");
                        if (!file.exists()) {
                                return ;
                        }
                        OutputStreamWriter out = null;
                        try {
                                out = new OutputStreamWriter(new FileOutputStream(file));
                                out.write(0);
                                out.flush();
                        } catch (FileNotFoundException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        } finally {
                                if (null != out) {
                                        try {
                                                out.close();
                                        } catch (IOException e) {
                                                e.printStackTrace();
                                        }
                                }
                        }
                        break;
                }
                default: {
                        Log.w(TAG, "Unknow type: " + type);
                        break;
                }
                }
        }

        @Override
        protected void onCreate() {
        }
}
